#include "mainwindow.h"
#include "ui_mainwindow.h"


void MainWindow::uptadeDisplay()
{
    ui->leScore_1->setText(QString("%1").arg(j1.score));
    ui->leScoreBoire_1->setText(QString("%1").arg(j1.gorgees));
    if(j1.action == ACTION_ATTAQUE)
        ui->pbAction_1->setText("ATTAQUE");
    else
        ui->pbAction_1->setText("DEFENDRE");

    ui->leScore_2->setText(QString("%1").arg(j2.score));
    ui->leScoreBoire_2->setText(QString("%1").arg(j2.gorgees));
    if(j2.action == ACTION_ATTAQUE)
        ui->pbAction_2->setText("ATTAQUE");
    else
        ui->pbAction_2->setText("DEFENDRE");
}

void MainWindow::endRound(void)
{
    if(j1.action == ACTION_ATTAQUE) // J1 PERDS
    {
        j2.score += j1.gorgees;
        j1.gorgees = 0;
        j2.gorgees = 0;
        j2.action = ACTION_ATTAQUE;
        j1.action = ACTION_DEFENDE;
    }
    else
    {
        j1.score += j2.gorgees;
        j1.gorgees = 0;
        j2.gorgees = 0;
        j1.action = ACTION_ATTAQUE;
        j2.action = ACTION_DEFENDE;
    }

    uptadeDisplay();


}

void MainWindow::newRound(void)
{


}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // Init variables

    j1.action = ACTION_ATTAQUE;
    j1.score = 0;
    j1.gorgees = 0;

    j2.action = ACTION_DEFENDE;
    j2.score = 0;
    j2.gorgees = 0;

    uptadeDisplay();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }

    connect(ui->pbAction_1, SIGNAL(clicked()), this, SLOT(pbActionJ1()));
    connect(ui->pbAction_2, SIGNAL(clicked()), this, SLOT(pbActionJ2()));
    connect(ui->pbNextRound, SIGNAL(clicked()), this, SLOT(pbNextRound()));



}


void MainWindow::pbNextRound()
{
    endRound();
}

void MainWindow::pbActionJ1()
{
    if(j1.action == ACTION_DEFENDE)
        return;
    j1.action = ACTION_DEFENDE;
    j2.action = ACTION_ATTAQUE;

    j2.gorgees++;
    uptadeDisplay();



}

void MainWindow::pbActionJ2()
{
    if(j2.action == ACTION_DEFENDE)
        return;
    j2.action = ACTION_DEFENDE;
    j1.action = ACTION_ATTAQUE;


    j1.gorgees++;
    uptadeDisplay();
}


